<template>
    <div>
        <div class="mine" v-if="this.phones !== null">
            <div class="header">
                <van-image
                    round
                    width="80px"
                    height="70px"
                    src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.bq233.com%2Fkanqq%2Fpic%2Fupload%2F2018%2F0807%2F1533622762937587.jpg&refer=http%3A%2F%2Fimg.bq233.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1641967008&t=1c3db469f4b06d9f4d21940c3bf7f492"
                />
                <div class="name">
                    <p>{{userMessage.userName}}</p>
                    <p>{{userMessage.businessName}}——{{userMessage.projectName}}</p>
                </div>
            </div>
            <div class="operation">
                <van-button type="default" to="/home">今日打卡</van-button>
                <van-button type="default" to="/record">我的记录</van-button>
            </div>
        </div>
        <div v-else>
            <div class="login_box">
                <div class="phoneINput">
                    <div class="input">
                        手机号<input type="text" maxlength="11" v-model="phone">
                    </div>
                    <div class="button">
                        <van-button color="#3FAC9D" type="primary" @click="handlerPunchCard">确认登入</van-button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import Vue from 'vue';
import { NavBar } from 'vant';
Vue.use(NavBar);
import { Button } from 'vant';
Vue.use(Button);
import { Dialog } from 'vant';
import { Image as VanImage } from 'vant';
import { Toast } from 'vant';

import {login} from '../../api/mine'
Vue.use(Toast);
Vue.use(VanImage);
    export default{
        data(){
           return{
               phone:'',
               phones:localStorage.getItem('phones'),
               userMessage:JSON.parse(localStorage.getItem('phones')) || []
           }
        },
        methods:{
            handlerPunchCard(){
                if(this.phone == ''){
                  Toast.fail('请输入正确的手机号')
                }else{
                    login({phone:this.phone}).then(res=>{
                        // 后端返回的数据存储在本地存储
                        localStorage.setItem('phones',JSON.stringify(res.data.data))
                        this.userMessage = res.data.data
                    })
                  this.phone = ''
                  Dialog.alert({
                      message: '登入成功',
                      }).then(() => {
                          location.reload()
                  });
                }
            }
        }
    }
</script>
<style lang="scss" scoped>
    *{
        margin: 0;
    }
    .mine{
        width: 100%;
        height:43px;
        background-color: #BBBBBB;
    }
    .header{
        background-color: white;
        display: flex;
        height: 180px;
        align-items: center;
        justify-content: center;
        .van-image{
            padding-right: 10px;
        }
        .name{
            display: flex;
            flex-direction: column;
            align-items: center;
            p{
              font-size: 20px;
              padding-bottom: 10px;
            }
        }
    }
    .operation{
        margin-top: 5px;
        width: 100%;
        height: 190px;
        background-color: white;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        .van-button{
            width: 140px;
            height: 140px;
        }
    }
    .login_box{
        width: 100%;
        height: 600px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-evenly;
        .phoneINput{
            width: 80%;
            height: 260px;
            border: 1px solid #cacbcb;
            border-radius: 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-evenly;
            .input{
                color: #cacaca;
                width: 90%;
                border-bottom: 1px solid #cacaca;
                padding-bottom: 10px;
                input{
                    background-color: #f4f5f6;
                    width: 70%;
                    color: #3FAC9D;
                    border-width: 0px;
                    margin-left: 10px;
                }
            }
            .button{
                .van-button{
                    width: 400px;
                    height: 50px;
                    border-radius: 30px;
                    span{
                        font-size: 15px;
                    }
                }
            }
        }
    }
</style>
